package com.skytech.tax.customsdatawarehouse.singlewindow.revokelist.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.revokelist.domain.entity.CusAutoRevokeListSimple;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lianhong
 * @since 2021/11/9
 */
@Repository
public class CusAutoRevokeListSimpleDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String saveRevokeListSimpleSql = "INSERT INTO sw_cus_auto_revoke_list_simple (decModSeqNo) VALUES (:decModSeqNo)";

    public void save(CusAutoRevokeListSimple simple) {
        namedParameterJdbcTemplate.update(this.saveRevokeListSimpleSql, new BeanPropertySqlParameterSource(simple));
    }

    private String deleteRevokeListSimpleSql = "DELETE FROM sw_cus_auto_revoke_list_simple WHERE decModSeqNo = ?";

    public void delete(String decModSeqNo) {
        this.namedParameterJdbcTemplate.getJdbcOperations().update(this.deleteRevokeListSimpleSql, decModSeqNo);
    }

    private String existsRevokeListSql = "SELECT count(1) FROM sw_cus_auto_revoke_list_simple WHERE decModSeqNo = ?";

    public boolean exists(String decModSeqNo) {
        Integer count = namedParameterJdbcTemplate.getJdbcOperations()
                .queryForObject(this.existsRevokeListSql, new Object[]{decModSeqNo}, Integer.class);
        return count != null && count > 0;
    }

    private String existsRevokeListSimpleSql = "SELECT decModSeqNo FROM sw_cus_auto_revoke_list_simple WHERE decModSeqNo in (:decModSeqNos)";

    public List<String> exists(List<String> decModSeqNos) {
        Map<String, Object> params = new HashMap<>();
        params.put("decModSeqNos", decModSeqNos);
        return namedParameterJdbcTemplate.queryForList(this.existsRevokeListSimpleSql, params, String.class);
    }
}
